package com.sinoeyes.platform.handle;

import com.sinoeyes.agreement.po.AgreementPo;
import com.sinoeyes.agreement.po.AgreementfinedetailPo;
import com.sinoeyes.agreement.po.AttachmentInfoPo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 自定义sql拦截处理
 * @author arise.hao
 */
@Slf4j
//@Component
//@Intercepts({@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})})
public class DbDataHandlerProcess implements Interceptor {
    private ThreadLocal<SimpleDateFormat> simpleDateFormat = new ThreadLocal<>();

    private void getInstance() {
        if(simpleDateFormat.get() == null) {
            simpleDateFormat.set(new SimpleDateFormat("yyyy-MM-dd"));
        }
    }

//    原生态原始实现方式方案，有效！太啰嗦局限 @Override
//    public Object intercept(Invocation invocation) throws Throwable {
//        Object proceed = invocation.proceed();
//        if(proceed instanceof List){
//            getInstance();
//            ArrayList resultList = (ArrayList) proceed;
//            for (Object o : resultList) {
//                Field[] declaredFields = o.getClass().getDeclaredFields();
//                for (Field declaredField : declaredFields) {
//                    if (declaredField.getClass().equals(Date.class)) {
//                        declaredField.getName();
//                        String convertDate = simpleDateFormat.format(declaredField);
//                    }
//                }
//                if(o.getClass().equals(Agreement.class)) {
//                    Agreement agreement = (Agreement) o;
//                    AttachmentInfo attachmentInfo = agreement.getAttachmentInfo();
//                    Agreementfinedetail agreementfinedetail = agreement.getAgreementfinedetail();
//                    agreement.setBondAmountFix(agreementfinedetail.getBondAmountFix() != null ? agreementfinedetail.getBondAmountFix() : 0.0);
//                    agreement.setBondAmountPlan(agreementfinedetail.getBondAmountPlan() != null ? agreementfinedetail.getBondAmountPlan() : 0.0);
//                    agreement.setAttachmentStatus(attachmentInfo.getAttachmentStatus ()!= null ? attachmentInfo.getAttachmentStatus() : "--");
//                    agreement.setAttachmentId(attachmentInfo.getId());
//                    agreement.setStrBeginTime(simpleDateFormat.format(agreement.getBeginDate()));
//                    agreement.setStrEndTime(simpleDateFormat.format(agreement.getEndDate()));
//                    agreement.setStrUpdateTime(simpleDateFormat.format(agreement.getUpdateTime()));
//                    agreement.setBeginDate(null);
//                    agreement.setEndDate(null);
//                    agreement.setUpdateTime(null);
//                    agreement.setAgreementfinedetail(null);
//                    agreement.setAttachmentInfo(null);
//                }
//                else if(o.getClass().equals(DimensionInfo.class)) {
//                    DimensionInfo dimensionInfo = (DimensionInfo) o;
//                    dimensionInfo.setStrCreateTime(simpleDateFormat.format(dimensionInfo.getCreateTime()));
//                    dimensionInfo.setStrUpdateTime(simpleDateFormat.format(dimensionInfo.getUpdateTime()));
//                    dimensionInfo.setCreateTime(null);
//                    dimensionInfo.setUpdateTime(null);
//                }
//                else if(o.getClass().equals(ProductInfo.class)) {
//                    ProductInfo productInfo = (ProductInfo) o;
//                    productInfo.setStrCreateTime(simpleDateFormat.format(productInfo.getCreateTime()));
//                    productInfo.setStrUpdateTime(simpleDateFormat.format(productInfo.getUpdateTime()));
//                    productInfo.setCreateTime(null);
//                    productInfo.setUpdateTime(null);
//                }
//            }
//        }
//        return proceed;
//    }


    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object proceed = invocation.proceed();
        if(proceed instanceof List){
            getInstance();
            ArrayList resultList = (ArrayList) proceed;
            for (Object o : resultList) {
                // 协议目前比较特殊，单独列出
                if(o.getClass().equals(AgreementPo.class)) {
                    AgreementPo agreement = (AgreementPo) o;
                    AttachmentInfoPo attachmentInfo = agreement.getAttachmentInfo();
                    AgreementfinedetailPo agreementfinedetail = agreement.getAgreementfinedetail();
                    agreement.setBondAmountFix(agreementfinedetail.getBondAmountFix() != null ? agreementfinedetail.getBondAmountFix() : 0.0);
                    agreement.setBondAmountPlan(agreementfinedetail.getBondAmountPlan() != null ? agreementfinedetail.getBondAmountPlan() : 0.0);
                    agreement.setAttachmentStatus(attachmentInfo.getAttachmentStatus ()!= null ? attachmentInfo.getAttachmentStatus() : "--");
                    agreement.setAttachmentId(attachmentInfo.getId());
                    agreement.setBeginDate(null);
                    agreement.setEndDate(null);
                    agreement.setUpdateTime(null);
                    agreement.setAgreementfinedetail(null);
                    agreement.setAttachmentInfo(null);
                    continue;
                }
                // 使用规则，bean内必须按照规则指定filedName的属性，strDest需要包含filedName的连续单词
//                SqlDateUtil.realConvertDate(o, simpleDateFormat, "strCreateTime", "create");
//                SqlDateUtil.realConvertDate(o, simpleDateFormat, "strBeginTime", "begin");
//                SqlDateUtil.realConvertDate(o, simpleDateFormat, "strEndTime", "end");
//                SqlDateUtil.realConvertDate(o, simpleDateFormat, "strUpdateTime", "update");
//                SqlDateUtil.realConvertDate(o, simpleDateFormat, "strExpireTime", "expire");
//                SqlDateUtil.realConvertDate(o, simpleDateFormat, "strStartTime", "start");
            }
            remove();
        }
        return proceed;
    }

    @Override
    public Object plugin(Object o) {
        return Plugin.wrap(o, this);
    }

    private void remove() {
        if(this.simpleDateFormat.get() != null) {
            simpleDateFormat.remove();
        }
    }

    @Override
    public void setProperties(Properties properties) {
        log.info("############&&&&:::" + properties);
    }
}
